﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Adex;
using Telerik.Web.UI;
using SubSonic.Sugar;
using System.Security;
//using Artem.Web.UI.Controls;
using MediaMonitor.Common;


namespace MediaMonitor.Users
{
    public partial class Report_Outdoor_Map : Adex.BasePage
    {
        //DateTime sDate = Convert.ToDateTime("01/01/2000"), eDate = Convert.ToDateTime("01/01/2000");
        int mediaTypeCode = 0;
        string mediaCodes = "";
        int programCode = 0;
        int sectorCode = 0, subSectorCode = 0, subSector2Code = 0, categoryCode = 0;
        int advertiserCode = 0, brandCode = 0, productCode = 0, versionCode = 0, agencyCode = 0;
        int sTime = 0, eTime = 0;
        int size = 0;
        string quality = "";
        int typeofads = 0;
        int sPrice = 0, ePrice = 0;
        bool firstAppearanceOnly = false;
        string cols = "";

        protected void Page_Init(object sender, EventArgs e)
        {

        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!(IsPostBack))
            {
                BindMap();
            }
        }

        private void PrepareGridColumns(string cols)
        {



            //radGrid.Columns.FindByUniqueName("RFDate").Visible = cols.Contains("RFDate");
            //radGrid.Columns.FindByUniqueName("MediaType").Visible = cols.Contains("MediaTypeName");
            //radGrid.Columns.FindByUniqueName("Media").Visible = cols.Contains("MediaName"); 

            //radGrid.Columns.FindByUniqueName("Sector").Visible = cols.Contains(" SectorName"); //a space before sectorname to keep a difference from SubSectorName 
            //radGrid.Columns.FindByUniqueName("SubSector").Visible = cols.Contains("SubSectorName");
            //radGrid.Columns.FindByUniqueName("Category").Visible = cols.Contains("CategoryName");

            //radGrid.Columns.FindByUniqueName("Media").Visible = cols.Contains("MediaName");
            //radGrid.Columns.FindByUniqueName("Agency").Visible = cols.Contains("AgencyName");
            //radGrid.Columns.FindByUniqueName("Advertiser").Visible = cols.Contains("AdvertiserName");
            //radGrid.Columns.FindByUniqueName("Brand").Visible = cols.Contains("BrandName");
            //radGrid.Columns.FindByUniqueName("Product").Visible = cols.Contains("ProductName");
            //radGrid.Columns.FindByUniqueName("ProdVersion").Visible = cols.Contains("ProdVersionName");
            //radGrid.Columns.FindByUniqueName("STime").Visible = cols.Contains("TimeStartFormatted");
            //radGrid.Columns.FindByUniqueName("ETime").Visible = cols.Contains("TimeEndFormatted");

            //radGrid.Columns.FindByUniqueName("Size").Visible = cols.Contains("Size");
            //radGrid.Columns.FindByUniqueName("Quality").Visible = cols.Contains("Quality");
            //radGrid.Columns.FindByUniqueName("TypeOfAds").Visible = cols.Contains("TypeOfAds");
            //radGrid.Columns.FindByUniqueName("VisibilityCode").Visible = cols.Contains("VisibilityCode");
            //radGrid.Columns.FindByUniqueName("CarrierCode").Visible = cols.Contains("CarrierCode");
            //radGrid.Columns.FindByUniqueName("BetweenAvenue").Visible = cols.Contains("BetweenAvenue");

            //radGrid.Columns.FindByUniqueName("RealCost").Visible = cols.Contains("RealCost"); 

            lnkGrp_Adv.Visible = cols.Contains("AdvertiserName");
            lnkGrp_Agnc.Visible = cols.Contains("AgencyName");
            lnkGrp_Brnd.Visible = cols.Contains("BrandName");
            lnkGrp_Prod.Visible = cols.Contains("ProductName");
            lnkGrp_Media.Visible = cols.Contains("MediaName");
            //lnkGrp_Prog.Visible = cols.Contains("ProgramName");


        }

        private void BindMap()
        {
            string sDate = Utility.GetRequestedVal("sd", "01_01_2000").Replace("_", "/");
            string eDate = Utility.GetRequestedVal("ed", "01_01_2000").Replace("_", "/");

            mediaTypeCode = Utility.ToInt(Utility.GetRequestedVal("mtc", "0"));

            bool searchFromAllMedia = Utility.ToBool(Utility.GetRequestedVal("mc", "false"));
            mediaCodes = searchFromAllMedia == false ? Session["SearchMediaCodes_outdoor"].ToString() : "";

            programCode = Utility.ToInt(Utility.GetRequestedVal("prg", "0"));

            sectorCode = Utility.ToInt(Utility.GetRequestedVal("s", "0"));
            subSectorCode = Utility.ToInt(Utility.GetRequestedVal("ss", "0"));
            categoryCode = Utility.ToInt(Utility.GetRequestedVal("cc", "0"));

            advertiserCode = Utility.ToInt(Utility.GetRequestedVal("adv", "0"));
            brandCode = Utility.ToInt(Utility.GetRequestedVal("brnd", "0"));
            productCode = Utility.ToInt(Utility.GetRequestedVal("prod", "0"));
            versionCode = Utility.ToInt(Utility.GetRequestedVal("ver", "0"));
            agencyCode = Utility.ToInt(Utility.GetRequestedVal("agn", "0"));

            sTime = Utility.ToInt(Utility.GetRequestedVal("st", "0"));
            eTime = Utility.ToInt(Utility.GetRequestedVal("et", "0"));



            size = Utility.ToInt(Utility.GetRequestedVal("size", "0"));
            quality = Utility.GetRequestedVal("quality", "");

            typeofads = Utility.ToInt(Utility.GetRequestedVal("typeofads", "0"));


            sPrice = Utility.ToInt(Utility.GetRequestedVal("sp", "0"));
            ePrice = Utility.ToInt(Utility.GetRequestedVal("ep", "0"));

            firstAppearanceOnly = Utility.ToBool(Utility.GetRequestedVal("fa", "0"));

            cols = Utility.GetRequestedVal("cols", "ProdVersionCode, ProductCode, ProdVersionName, ProductName, Price");

            cols = "MediaTypeCode, ProdVersionCode, ProductCode, OutdoorFile, NewOutdoorFile, " + cols;

            if (sDate != "1/1/2000" || eDate != "1/1/2000")
                cols = "RFDate, " + cols;

            if (!firstAppearanceOnly)
            {
                // cols += ", ShortVideo, FullVideo, ShortVideoFile, FullVideoFile, Duration, Price";
                cols += ", Price";
            }
            else
            {
                cols = cols.Replace("RFDate,", "");
                //cols += ", RFDate, ShortVideo, FullVideo, ShortVideoFile, FullVideoFile, Duration, Price";
                cols += ", RFDate, Price";
            }

            cols += ", NewOutDoorFile";
           
            
            DataTable dt = SPs.GetReportOutDoorMap(AppUser.CurrUserID, Utility.ToDateTime(sDate), Utility.ToDateTime(eDate), mediaTypeCode, mediaCodes, sectorCode, subSectorCode, categoryCode, advertiserCode, brandCode, productCode, versionCode, agencyCode, sPrice, ePrice, size, quality, typeofads, firstAppearanceOnly).GetDataSet().Tables[0];


            if (dt.Rows.Count == 0)
            {
                lblMsg.Text = "No Record Found...";
                odMap.Visible = false;
                return;
            }



            //load map 
            odMap.DataSource = dt;
            odMap.LoadMap("GMap", 900, 550, 7);
            odMap.Visible = true;

            // outdoormap.DataSource= 
            //radGrid.DataSource = dt; 
            PrepareGridColumns(cols);

        }



        //protected void radGrid_ItemCommand(object source, GridCommandEventArgs e)
        //{
        //    if (e.CommandName == "Add2Cart")
        //    {

        //        int pvCode = Utility.ToInt(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ProdVersionCode"].ToString());
        //        int pdCode = Utility.ToInt(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ProductCode"].ToString());
        //        decimal price = Utility.ToDecimal(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Price"]);
        //        string outdoorFile = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["NewOutDoorFile"].ToString();

        //        hdnPV.Value = pvCode.ToString();
        //        hdnPD.Value = pdCode.ToString();
        //        hdnPrice.Value = price.ToString();
        //        hdnOutDoorFile.Value = outdoorFile;



        //        int existingOrderWithSameItem = Cart.FindOrderByProdVersion(pvCode, AppUser.CurrUserID);

        //        if (existingOrderWithSameItem > 0)
        //        {
        //            string warningMsg = "The video you want to purchase is already present in your Order #<b>" + existingOrderWithSameItem.ToString() + "</b>.<br><br> Do you still want to purchas this Outdoor file?";
        //            ltrWarningOrderNum.Text = warningMsg;
        //            pnl.Visible = true;
        //        }
        //        else
        //        {
        //            Add2Cart(pvCode, pdCode, price, outdoorFile);
        //            //ShowPop(this.Page, false);

        //        }


        //    }//end Add2Cart
        //}

        //private void Add2Cart(int pvCode, int pdCode, decimal price, string outdoorFile)
        //{


        //    VProdVersion pv = new VProdVersion("ProdVersionCode", pvCode);

        //    string pvName = pv.ProdVersionName;
        //    string pdName = pv.ProductName;
        //    string svf = Utility.ToStr(pv.ShortVideoFile);

        //    string fvf = Utility.ToStr(pv.FullVideoFile);

        //    bool sv = Utility.ToBool(pv.ShortVideo);

        //    bool fv = Utility.ToBool(pv.FullVideo);

        //    string prFile = "";
        //    string odFile = outdoorFile;

        //    string rdFile = "";
        //    string cbFile = "";
        //    string inFile="";
        //    int mtCode = Utility.ToInt(MediaType.Query().SetSelectList("MediaTypeCode").WHERE("MediaTypeName", "OutDoor").ExecuteScalar());

        //    if (Cart.AddItem(Cart.CreateCartItem(mtCode, pv.ProdVersionID, pvName, pv.ProductID, pdName, price, svf, fvf, sv, fv, prFile, odFile, rdFile, cbFile, inFile)) == true)
        //    {
        //        AjaxFuncs.MsgBox(this.Page, "Selected file added to your cart successfully");
        //    }
        //    else
        //    {
        //        AjaxFuncs.MsgBox(this.Page, "Already added to your cart");
        //    }

        //}


        protected void lnkGrp_Clicked(object sender, EventArgs e)
        {


            LinkButton lnk = ((LinkButton)(sender));
            string grp = lnk.CommandArgument;

            if (lnk.Text.StartsWith("Group"))
            {
                ////radGrid.MasterTableView.GroupByExpressions.Add(new GridGroupByExpression(grp + " Group By " + grp));
                //radGrid.MasterTableView.GroupLoadMode = GridGroupLoadMode.Server;
                //radGrid.MasterTableView.GroupsDefaultExpanded = true;

                // radGrid.MasterTableView.GroupByExpressions.Add(grp + " Group By " + grp);
                lnk.ForeColor = System.Drawing.Color.Red;
                lnk.Font.Bold = true;
                lnkClearGroup.Visible = true;
            }

            //radGrid.Rebind();

        }

        protected void lnkClearGroup_Clicked(object sender, System.EventArgs e)
        {
            //radGrid.MasterTableView.GroupByExpressions.Clear();
            //radGrid.Rebind();

            lnkGrp_Adv.ForeColor = lnkGrp_Agnc.ForeColor = lnkGrp_Brnd.ForeColor = lnkGrp_Media.ForeColor = lnkGrp_Prod.ForeColor = System.Drawing.Color.PowderBlue;

            lnkGrp_Adv.Font.Bold = lnkGrp_Agnc.Font.Bold = lnkGrp_Brnd.Font.Bold = lnkGrp_Media.Font.Bold = lnkGrp_Prod.Font.Bold = true;

            lnkClearGroup.Visible = false;
        }




        protected void btnCancelPurchase_Clicked(object sender, EventArgs e)
        {
            pnl.Visible = false;
        }

        protected void btnContinuePurchase_Clicked(object sender, EventArgs e)
        {
            pnl.Visible = false;
            //Add2Cart(Utility.ToInt(hdnPV.Value), Utility.ToInt(hdnPD.Value), Utility.ToDecimal(hdnPrice.Value), hdnOutDoorFile.Value);


        }

        public static void ShowPop(Page aspxPage, bool show)
        {
            try
            {

                AjaxControlToolkit.ModalPopupExtender screen = (AjaxControlToolkit.ModalPopupExtender)aspxPage.FindControl("MPE");


                if ((screen == null))
                    return;

                if (show == true)
                {
                    screen.Show();

                }
                else
                {
                    screen.Hide();
                }
            }
            catch
            {
            }
        }
    }
}